package com.estasenposse.tdam.ui.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.estasenposse.tdam.entities.Noticia;
import com.example.estasenposse.R;

public class NewsAdapter extends BaseAdapter {
	
	private ArrayList<Noticia> mNews;
	private LayoutInflater mInflater;
	private Context context;
	

	
	public NewsAdapter(Context context) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;
	}
	
	public void setNews(ArrayList<Noticia> news) {
		mNews = news;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		int count =0;
		if(mNews != null) {
			count = mNews.size();
		}
		return count;
	}

	@Override
	public Object getItem(int position) {
		Noticia n = null;
		if(mNews != null) {
			n = mNews.get(position);
		}
		return n;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public int getNoteId(int position) {
		return Integer.parseInt(mNews.get(position).getID());
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.news_item_list, parent, false);
			holder = new ViewHolder();
			holder.txtTipo = (TextView) convertView.findViewById(R.id.txt_tipo);
			holder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title);
			holder.txtSubtitle = (TextView) convertView.findViewById(R.id.txt_sub_title);
			holder.ivNote = (ImageView) convertView.findViewById(R.id.iv_note);
			holder.pgLoading = (ProgressBar) convertView.findViewById(R.id.pg_loading);
			
			 final float[] roundedCorners = new float[]{ 5, 5, 5, 5, 5, 5, 5, 5 };
			ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
			String MyColor = "#FF00FF";
			pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		
		Noticia note = (Noticia) getItem(position);
		if(note != null) {
			holder.txtTitle.setText(note.getTitle());	
			holder.txtTipo.setText(note.getTipo());
			if(note.getSubtitle() != null) {
				holder.setId(Integer.parseInt(note.getID()));
				holder.txtSubtitle.setText(note.getSubtitle());
				holder.txtSubtitle.setVisibility(View.VISIBLE);
			} else {
				holder.txtSubtitle.setVisibility(View.INVISIBLE);
			}			
			String url = note.getImage();
			
			String[] part = url.split("/FOTOPRINCIPAL/");

			String aux = part[1];
			
			ImageDownloader downloader = new ImageDownloader(holder.ivNote, holder.pgLoading, context);
			downloader.execute("http://www.estasenposse.com/FOTOPRINCIPAL/Th_"+aux);
			
		}
		
		return convertView;
	}
	

	class ViewHolder {
		int id;
		TextView txtTipo;
		TextView txtTitle;
		TextView txtSubtitle;
		ImageView ivNote;
		ProgressBar pgLoading;		
		private void setId(int id){
			this.id = id;
		}
	}
	
}
